const CompressionWebpackPlugin = require('compression-webpack-plugin')
const path = require('path')
const proxyUrl = 'http://www.yunids.com:8082'
// const proxyUrl = 'http://192.168.1.3:18023'

function resolve (dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  // 基本路径
  publicPath: process.env.VUE_APP_PUBLIC_PATH,
  // 输出文件目录
  outputDir: 'dist',
  // 配置js、css等文件夹的二级目录位置，不设置则会在dist下生成4个文件夹
  assetsDir: 'static',
  // webpack-dev-server相关配置
  devServer: {
    disableHostCheck: true,
    // 修改端口
    port: 9000,
    open: true,
    host: '0.0.0.0',
    // host: '192.168.1.25',
    https: false,
    hotOnly: false,
    proxy: {
      // changexxx-api/login=>mock/login
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      '/dingtalk': {
        target: 'http://120.24.252.203:12345/ktfw-boot',
        ws: true,
        changOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      [process.env.VUE_APP_BASE_API + process.env.VUE_APP_OATH_API]: {
        target: proxyUrl,
        ws: true,
        changOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      [process.env.VUE_APP_BASE_API + process.env.VUE_APP_AUTHORITY_API]: {
        target: proxyUrl,
        ws: true,
        changOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      // [process.env.VUE_APP_CDHNTECH_API]: {
      //   target: proxyUrl,
      //   ws: true,
      //   changOrigin: true,
      //   pathRewrite: {
      //     '^/api': ''
      //   }
      // },
      [process.env.VUE_APP_BASE_API]: {
        target: proxyUrl,
        ws: true,
        changOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  },
  pluginOptions: {
    plugin: [
      new CompressionWebpackPlugin({
        test: /\.js$|\.html$|\.css/, // 匹配文件后缀名
        threshold: 10240, // 对超过10kb文件进行压缩
        deleteOriginalAssets: false // 是否删除源文件
      })
    ]
  },
  // 配置antdv依赖的less
  css: {
    loaderOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },
  configureWebpack: config => {
    // 生产环境开启gzip压缩
    if (process.env.NODE_ENV === 'production') {
      return {
        resolve: {
          alias: {
            '@': resolve('src')
          }
        },
        externals: {
          T: 'T', // 天地图
          BMapGL: 'BMapGL' // 百度地图
        }
      }
    } else {
      return {
        resolve: {
          alias: {
            '@': resolve('src')
          }
        },
        externals: {
          T: 'T', // 天地图
          BMapGL: 'BMapGL' // 百度地图
        }
      }
    }
  },
  chainWebpack: config => {
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()

    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
  }
}
